"""
装饰器

@wraps(func) 保留 原函数的信息
"""
import time
from functools import wraps


def calculate_time(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        t1 = time.perf_counter()
        func(*args, **kwargs)
        print("consume time: ", time.perf_counter() - t1)

    return wrapper


def calculate_time_name(name):
    def calculate(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            print("name: ", name)
            t1 = time.perf_counter()
            func(*args, **kwargs)
            print("consume time: ", time.perf_counter() - t1)

        return wrapper
    return calculate


# Define a function called my_func that takes a parameter num
@calculate_time
def my_func(num):
    # Initialize a variable num_sum to 0
    num_sum = 0
    # Loop through the range of num
    for n in range(num):
        # Add n to num_sum
        num_sum += n
    # Print the sum of the numbers
    print("sum:", num_sum)


@calculate_time_name("jack")
def my_func2(num):
    num_sum = 0
    for n in range(num):
        num_sum += n
    print("sum:", num_sum)


def re_calculate_time(func):
    @wraps(func)  # 保留原函数的信息
    def wrapper(*args, **kwargs):
        t1 = time.perf_counter()
        func(*args, **kwargs)
        print(f"time: {time.perf_counter() - t1}")
    return wrapper


@re_calculate_time
def my_func3(num):
    num_sum = 0
    for n in range(num):
        num_sum += n
    print("sum:", num_sum)

# f1 = calculate_time(my_func)
# f1(100)

my_func(100)
# 不加wraps，打印该调用，丢失了原来的信息
# <function calculate_time.<locals>.wrapper at 0x0000020F178A75E0>
# 加上后，打印出来的是 <function my_func at 0x0000025B297675E0>
print(my_func)

my_func2(1000)

print()
my_func3(1000)
print(my_func3)